package com.aiti.lulian.controller;

import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.annotates.AddGroup;
import com.aiti.lulian.annotates.EditGroup;
import com.aiti.lulian.dto.CustomerContactsDto;
import com.aiti.lulian.entity.CustomerContactsRelation;
import com.aiti.lulian.service.ICustomerContactsRelationService;
import com.aiti.lulian.service.ICustomerContactsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 联系人控制层
 */
@Slf4j
@Api(tags = "联系人控制层")
@RestController
@RequestMapping("customerContact")
public class CardCustomerContactController extends WebController {


    @Autowired
    private ICustomerContactsService customerContactsService;
    @Autowired
    private ICustomerContactsRelationService customerContactsRelationService;




    @ApiOperation("新建")
    @PostMapping
    public Message insert(@Validated(AddGroup.class) @RequestBody CustomerContactsDto dto) {
        if (StringUtils.isEmpty(dto.getUserId())) {
            dto.setCreator(findUserId());
            dto.setUserId(findUserId());
        }else{
            dto.setCreator(dto.getUserId());
        }
        return customerContactsService.insertContact(dto);
    }

    @ApiOperation("编辑")
    @PutMapping
    public Message update(@Validated(EditGroup.class) @RequestBody CustomerContactsDto dto) {
        if (StringUtils.isEmpty(dto.getUserId())) {
            dto.setUpdateBy(findUserId());
            dto.setUserId(findUserId());
        }else{
            dto.setUpdateBy(dto.getUserId());
        }
        return customerContactsService.updateContact(dto);
    }


    @ApiOperation("查询联系人详情")
    @GetMapping("/detail")
    public Message queryBoDetailsByBaseId(@RequestParam("baseId") String baseId) {
        return customerContactsService.detailInfo(baseId);
    }

    @ApiOperation("联系人列表分页")
    @GetMapping("/page")
    public Message page(CustomerContactsDto dto) {
        if (StringUtils.isEmpty(dto.getUserId())) {
            dto.setUserId(findUserId());
        }
        Page page = startPage(dto);
        return customerContactsService.contactPage(page, dto);
    }

    @ApiOperation("删除合同")
    @PostMapping("/delete")
    @Transactional(rollbackFor = Exception.class)
    public Message delete(@RequestBody CustomerContactsDto dto) {
        List<String> idList = dto.getIdList();
        if(!CollectionUtils.isEmpty(idList)){
            customerContactsService.removeByIds(idList);
            customerContactsRelationService.remove(new LambdaQueryWrapper<CustomerContactsRelation>().in(CustomerContactsRelation::getContactId, idList));
        }
        return Message.success();
    }

    @ApiOperation("列表批量编辑")
    @PutMapping("/updateBatch")
    public Message updateBatch(@RequestBody CustomerContactsDto dto) {
        return customerContactsService.updateListCustomerContact(dto);
    }

    @ApiOperation("查询客户的联系人列表")
    @GetMapping("/list")
    public Message list(@RequestParam("customerId") String customerId,
                        @RequestParam(value = "userId", required = false) String userId) {
        if (StringUtils.isEmpty(userId)) {
            if (StringUtils.isEmpty(findUserId())) {
                return Fail("请先登录");
            }
        }
        return Success(customerContactsService.getList(customerId));
    }

    @ApiOperation("批量修改客户联系人信息")
    @PutMapping("/batch")
    public Message updateCustomerContactsBatch(@RequestBody CustomerContactsDto dto) {
        String userId = dto.getUserId();
        if (StringUtils.isEmpty(userId)) {
            userId = findUserId();
            if (StringUtils.isEmpty(userId)) {
                return Fail("请先登录");
            }
            dto.setUserId(userId);
        }
        return customerContactsService.updateCustomerContactsBatch(dto);
    }
}